package org.jeecg.modules.utils;

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.ptr.IntByReference;
import org.jeecg.modules.bean.entity.patient.Patient;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;

public class Person {

	private String sysName = System.getProperty("os.name");

	public Person(String path) {

	}

    public interface RdcardDll extends Library {
        File f = new File("D:\\idDll");

        RdcardDll bell = (RdcardDll) Native.loadLibrary(f.getAbsolutePath() + "\\RdCard", RdcardDll.class);

        int UCommand1(String pCmd, IntByReference prag0, IntByReference prag1,
                      byte[] prag2);

		int GetName(byte[] buf);

		int GetSexGB(byte[] buf);

		int GetAddr(byte[] buf);

		int GetIDNum(byte[] buf);
	}

	public Patient open() throws UnsupportedEncodingException, ParseException {
		Patient patient = new Patient();
		String aa;
		aa = String.format("%c", 0x41);

		IntByReference bb = new IntByReference();
		bb.setValue(0);
		IntByReference cc = new IntByReference();
		cc.setValue(8811);


		byte[] dd = {0x02, 0x27, 0x00, 0x00};    //9986

		System.out.println(RdcardDll.bell.UCommand1(aa, bb, cc, dd)); // 锟斤拷始锟斤拷

		aa = String.format("%c", 0x43);
		System.out.println(RdcardDll.bell.UCommand1(aa, bb, cc, dd));

		aa = String.format("%c", 0x49);

		String strPara2 = "D:\\Download\\" + "\0";
		byte[] bytePara2 = strPara2.getBytes("GBK");

		int flag = RdcardDll.bell.UCommand1(aa, bb, cc, bytePara2);


		//		当身份证中没有指纹数据时，返回 62171 为成功；
		//		当身份证中含有指纹数据时，返回 62172 为成功；
		//		当读取外国人永久居留证时，返回 62173 为成功
		if ((flag == 62171) || (flag == 62172) || flag == 62173) {

			byte[] name1 = new byte[20];
			RdcardDll.bell.GetName(name1);
			String name = new String(name1, "GBK");
			name = name.replaceAll("\\s", "").trim();
			name = reEncoding(name, "UTF-8");
			patient.setName(name);

			byte[] sex1 = new byte[20];
			RdcardDll.bell.GetSexGB(sex1);
			String sex = new String(sex1, "GBK");
			sex = sex.replaceAll("\\s", "").trim();
			sex = reEncoding(sex, "UTF-8");
			if ("男".equals(sex)) {
				sex = "1";
			} else if ("女".equals(sex)) {
				sex = "2";
			}
			patient.setSexno(sex);

			byte[] addr = new byte[50];
			RdcardDll.bell.GetAddr(addr);
			String add = new String(addr, "GBK");
			add = add.replaceAll("\\s", "").trim();
			add = reEncoding(add, "UTF-8");
			patient.setAddress( add);

			byte[] idNum = new byte[50];
			RdcardDll.bell.GetIDNum(idNum);
			String id = new String(idNum, "GBK");
			id = id.replaceAll("\\s", "").trim();
			id = reEncoding(id, "UTF-8");
			patient.setCertificateno(id);

		}
		return patient;
	}

	public String reEncoding(String text, String newEncoding) {
		String rs = null;
		try {
			rs = new String(text.getBytes(), newEncoding);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rs;
	}

	public static void main(String[] args) throws UnsupportedEncodingException, ParseException {
		Person person = new Person("D://Download//");
		Patient patient = person.open();
		System.out.println("patient = " + patient);
	}

}